﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using ZyGames.Framework.Net;
using ZyGames.Framework.RPC.Sockets;
using ZyGames.Framework.Common.Log;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 房主点击开局请求(保险人页面的开始按钮),向所有玩家广播10038开始进入游戏Table页面响应
	/// </summary>
	public class Action10037 : BaseAction
	{
		private int _tableId;
		private TableData _currentTable;

		public Action10037(HttpGet actionGetter)
			: base((short)ActionType.DZEnterRoomRequest, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableID", ref _tableId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			try
			{
				_currentTable = GameRoom.Current.GetTableData(_tableId);
				if (_currentTable == null)
				{
					Tool.WriteErrorLine("消息10037获得房间不存在");
					return false;
				}
				return true;
			}
			catch (Exception ex)
			{
				Tool.WriteErrorLine("10037消息异常:" + ex.ToString());
				return false;
			}
		}

		public override void TakeActionAffter(bool state)
		{
			if (_currentTable != null)
			{
				//设置房间进入方法页面状态
				_currentTable.IsChangePlayState = true;
				var parameters = new Parameters();
				parameters["TableID"] = _tableId;
				GameTable.Current.SyncNotifyAction((int)ActionType.DZBroadcastEnterRoomRequest, _currentTable, parameters, t =>
				{
					TraceLog.WriteComplement("Action 10038 广播房主开局请求 桌子:{0}", _currentTable.TableId);
				});
			}
			base.TakeActionAffter(state);
		}
	}
}
